//
//  LoginPhoneView.swift
//  LeTu
//
//  Created by gy on 2023/11/30.
//

import UIKit

class LoginPhoneView: UIView {

    var placeholder: String {
        get { phoneTextField.placeholder ?? "" }
        set { phoneTextField.placeholder = newValue }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        configUI()
    }
    override func layoutSubviews() {
        super.layoutSubviews()
        
        DispatchQueue.main.async {
            self.backView.layerCornerRadius(self.backView.height)
        }
    }
    
    // MARK: - configUI
    func configUI() {
        
        addSubview(backView)
        backView.addSubview(areaBackView)
        areaBackView.addSubview(areaLabel)
        areaBackView.addSubview(areaArrowImgView)
        backView.addSubview(verticalLineView)
        backView.addSubview(phoneTextField)
        
        backView.snp.makeConstraints { make in
            make.left.bottom.right.equalToSuperview()
            make.top.equalToSuperview()
        }
        areaBackView.snp.makeConstraints { make in
            make.left.top.bottom.equalToSuperview()
        }
        areaLabel.snp.makeConstraints { make in
            make.left.equalTo(16)
            make.centerY.equalToSuperview()
        }
        areaArrowImgView.snp.makeConstraints { make in
            make.left.equalTo(areaLabel.snp.right).offset(2)
            make.width.height.equalTo(12)
            make.centerY.equalToSuperview()
            make.right.equalToSuperview().offset(-10)
        }
        verticalLineView.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.width.equalTo(1)
            make.height.equalTo(10)
            make.left.equalTo(areaBackView.snp.right)
        }
        phoneTextField.snp.makeConstraints { make in
            make.left.equalTo(verticalLineView.snp.right).offset(16)
            make.right.equalTo(-16)
            make.top.bottom.equalToSuperview()
        }
        
        areaLabel.setContentHuggingPriority(.required, for: .horizontal)
        
    }
    private lazy var backView: UIView = {
        let backView = UIView()
        backView.backgroundColor = .app_inputViewBackColor
        return backView
    }()
    private lazy var areaBackView = UIView()
    private lazy var areaLabel: UILabel = {
        let areaLabel = UILabel()
        areaLabel.text = "+86"
        areaLabel.textColor = .app_textDarkGrayColor
        areaLabel.font = kSetPingFangMedium(14)
        return areaLabel
    }()
    private lazy var areaArrowImgView: UIImageView = {
        let areaArrowImgView = UIImageView()
        areaArrowImgView.image = UIImage(named: "area_code_down_arrow")
        return areaArrowImgView
    }()
    private lazy var verticalLineView: UIView = {
        let verticalLineView = UIView()
        verticalLineView.backgroundColor = .app_lineDarkColor
        return verticalLineView
    }()
    private(set) lazy var phoneTextField: UITextField = {
        let phoneTextField = UITextField()
        phoneTextField.font = kSetPingFangMedium(14)
        phoneTextField.textColor = .app_textDarkGrayColor
        phoneTextField.keyboardType = .numberPad
        phoneTextField.returnKeyType = .done
        return phoneTextField
    }()
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

}
